package cn.wps.moffice.demo.fileManager;

import java.io.File;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wps.moffice.demo.R;
import cn.wps.moffice.demo.util.Util;

/**
 * @author ：chenyunbiao
 */
public class FileAdapter extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private File[] fileList;
	
	public FileAdapter(Context context, File[] files) {
		mInflater = LayoutInflater.from(context);
		fileList = files;
	}

	@Override
	public int getCount() {
		return fileList.length;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder viewHolder;
		if (convertView == null) {
			view = mInflater.inflate(R.layout.file_list_item, parent, false);
			
			viewHolder = new ViewHolder();
			viewHolder.itemText = (TextView) view.findViewById(R.id.item_text);
			viewHolder.itemIcon = (ImageView) view.findViewById(R.id.item_icon);
			
			view.setTag(viewHolder);
		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();
		}
		
		if (fileList.length != 0) {
			viewHolder.itemText.setText(fileList[position].getName());
		}
		
		if (fileList[position].isFile()) {
			if (Util.IsWPSFile(fileList[position])) {
				viewHolder.itemIcon.setImageResource(R.drawable.documents_icon_doc);
			} else if (Util.isExcelFile(fileList[position].getAbsolutePath())) {
				viewHolder.itemIcon.setImageResource(R.drawable.documents_icon_xls);
			} else if (Util.isPptFile(fileList[position].getAbsolutePath())) {
				viewHolder.itemIcon.setImageResource(R.drawable.documents_icon_ppt);
			} else if (Util.isPDFFile(fileList[position].getAbsolutePath())) {
				viewHolder.itemIcon.setImageResource(R.drawable.documents_icon_pdf);
			}   else {
				viewHolder.itemIcon.setImageResource(R.drawable.home_icon_other);
			}
		} else {
			viewHolder.itemIcon.setImageResource(R.drawable.format_folder);
		}
		return view;
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public Object getItem(int position) {
		return position;
	}
	
	static class ViewHolder {
		TextView itemText;
		ImageView itemIcon;
	}	
}